const http = require('http')
const fs = require('fs')
http
  .createServer(function(request, response) {
    if (request.url === '/') {
      const body = fs.readFileSync('./index.html', 'utf-8')
      response.writeHead(200, {
        'Content-Type': 'text/html'
      })
      response.end(body)
    }
    if (request.url === '/script.js') {
      const eTag = request.headers['if-none-match']
      if (eTag === '777') {
        response.writeHead(304, {
          'Content-Type': 'text/javascript',
          'Cache-Control': 'max-age=20000000, no-store',
          'Last-Modified': '123',
          Etag: '777'
        })
        response.end('666')
      } else {
        response.writeHead(200, {
          'Content-Type': 'text/javascript',
          'Cache-Control': 'max-age=20000000, no-store',
          'Last-Modified': '123',
          Etag: '777'
        })
        response.end(`console.log('hello world')`)
      }
    }
  })
  .listen(8888)

console.log('server at 8888')
